package util.apps;

import java.text.DecimalFormat;
import java.util.*;

import util.bio.calc.Alignment;
import util.bio.parsers.*;
import trans.roc.*;
import util.bio.seq.*;
import util.gen.*;
import util.bio.annotation.*;

import java.io.*;

import org.apache.commons.math.distribution.*;

import flanagan.analysis.Stat;
import trans.main.*;
import java.util.regex.*;

import edu.utah.seq.data.*;
/**For testing*/
public class Delme2 {



	public static void main(String[] args) {
		HashSet badEndos = IO.loadFileIntoHashSet(new File("/Users/nix/Desktop/badEndos.txt"));
		String[] endos = IO.loadFile(new File("/Users/nix/HCI/PIs/Bass/Annotation_CE6/ChrT/chrT.bed"));
		
		for (int i=0; i< endos.length; i++){
			String[] t = endos[i].split("\t");
			if (badEndos.contains(t[3]) == false) System.out.println(endos[i]);
		}
		
		
		/*
		Pattern pat = Pattern.compile(".+\\.\\w+\\.\\d+");
		
		
		try{
			BufferedReader in = new BufferedReader (new FileReader("/Users/nix/HCI/PIs/Bass/Annotation_CE6/UCSCTables/allAnnoCe6WS190.txt"));
			String[] tokens;
			String line;
			while ((line = in.readLine()) != null){
				tokens = line.split("\t");
				Matcher mat = pat.matcher(tokens[0]);
				if (mat.matches()){
					tokens[0] = tokens[0].substring(0, tokens[0].lastIndexOf(".")); 
				}
				System.out.println(Misc.stringArrayToString(tokens, "\t"));
			}
		} catch (Exception e){
			e.printStackTrace();
		}
		
		
		double totalInput = 9687768;
		double totalChip = 9687768	
;

		double numberWindowInputReads = 1;
		double numberWindowChipReads = 50;

		double sumWindowInputReads = 1;
		double sumWindowChipReads = 5;
		
		double proportionInput = sumWindowInputReads/numberWindowInputReads;
		double proportionChip = sumWindowChipReads/numberWindowChipReads;

		if (numberWindowInputReads !=0) proportionInput = sumWindowInputReads/numberWindowInputReads;
		if (numberWindowChipReads !=0) proportionChip = sumWindowChipReads/numberWindowChipReads;

		double expectation = (proportionInput * totalChip)/( (proportionInput * totalChip) + (proportionChip * totalInput) );
		
		int numberTrials = (int) ( numberWindowInputReads + numberWindowChipReads);

		//BinomialDistributionImpl b = new BinomialDistributionImpl(numberTrials, expectation);
		BinomialDistributionImpl b = new BinomialDistributionImpl(24, 0.3404219);
		
		try {
			System.out.println("xxxxx "+(1-b.cumulativeProbability(7)));
			
			double pval = 1-b.cumulativeProbability(numberWindowChipReads-1);
			System.out.println(totalChip+"\tTotalChipReads");
			System.out.println(numberWindowChipReads+"\tNumChIPReads");
			System.out.println("\t"+sumWindowChipReads+"\tSum");
			System.out.println("\t"+proportionChip+"\tProportion");
			System.out.println();
			System.out.println(totalInput+"\tTotalInputReads");
			System.out.println(numberWindowInputReads+"\tNumInputReads");
			System.out.println("\t"+sumWindowInputReads+"\tSum");
			System.out.println("\t"+proportionInput+"\tProportion");
			System.out.println();
			System.out.println(numberTrials+"\tNumReads\t"+expectation+"\tExpectation");
			System.out.println(pval+"\tPVal");
		}
			catch (Exception e){
				e.printStackTrace();
			};

		

*/
	}
}
	/*	

		//SAVE for spikes!
		//for each directory
			ArrayList<String>alignments = new ArrayList<String>();
			for (int x=5; x<= 100; x++){
				alignments.add("#Reads per Spike "+x);
				File groupDir = new File("/Users/nix/Desktop/Groups/G"+x);

				//get files
				File[] alignFiles = IO.extractFiles(groupDir);
				//for each file
				for (int i=0; i< alignFiles.length; i++){
					if (alignFiles[i].getName().startsWith("elandAlignments.txt") == false) continue;
					//load lines
					String[] lines = IO.loadFile(alignFiles[i]);
					//randomize
					Misc.randomize(lines, System.currentTimeMillis());
					//add lines 
					int numToFetch = x;
					System.out.println("NumToFetch "+numToFetch);
					for (int j=0; j< lines.length; j++) {
						if (lines[j].contains("chr")){
							alignments.add(lines[j]);
							numToFetch--;
						}
						if (numToFetch ==0) break;
					}
					if (numToFetch !=0) Misc.printExit("Too few in "+alignFiles[i]+" "+numToFetch);
				}
			}
			IO.writeArrayList(alignments, new File("/Users/nix/Desktop/Groups/spikes5-100.txt"));

	}


}


	 * 
	 * 
	 * String[] s = IO.loadFile(new File("/Users/nix/Desktop/numbers.txt"));
		String ss = Misc.stringArrayToString(s, ",");
		int[] num = Num.stringArrayToInts(ss, ",");
		int counter = 0;
		int group = 1;
		System.out.print("mkdir S"+group+"; for x in ");
		for (int i=0; i< num.length; i++){
			if (counter == 10){
				counter = 0;
				System.out.println("; do mv Spk$x S"+group+"; done");
				group++;
				System.out.print("mkdir S"+group+"; for x in ");
			}
			counter++;
			System.out.print(num[i]+" ");
		}

		try{
			BufferedReader in = new BufferedReader( new FileReader( new File(args[0])));
			PrintWriter out = new PrintWriter (new FileWriter (new File(args[0]+".bed")));
			String line;
			String[] tokens;
			Pattern ws = Pattern.compile("\\s+|:");
			while ((line = in.readLine()) != null){
				tokens = ws.split(line);
				String chr = tokens[3];
				int start = Integer.parseInt(tokens[4]);
				int stop = start + 26;
				String strand = "+";
				if (tokens[5].equals("R")) strand = "-";
				out.println(chr+ "\t" +start+ "\t" +stop+ "\t" +strand);
			}
			out.close();
			in.close();


		} catch (Exception e){
			e.printStackTrace();
		}*/
	/*
		File cela = new File("/Users/nix/Desktop/Ecoli/WT.cela");
		float[][] x = (float[][])IO.fetchObject(cela);
		for (int i=0; i< x.length; i++){
			for (int j=0; j< x[i].length; j++){
				x[i][j] = 1;
			}
		}
		String name = Misc.removeExtension(cela.getName());
		File blanked = new File (cela.getParentFile(), name+"Blk.cela");
		IO.saveObject(blanked, x);
		System.out.println(x[10][10]);
	}
	 */
	/*
		double numberOfPositions = 1;
		double total = 700;
		//Treat
		double obsT = 410;
		double expT = 0.6 * total;

		//Cont
		double obsC = total - obsT;
		double expC = total - expT;

		double x = Stat.chiSquareFreq(new double[]{obsT, obsC}, new double[]{expT, expC});
		//two tailed!
		double pval = (1- Stat.chiSquareCDF(x, 1)) * numberOfPositions;
		if (pval > 1) pval = 0;
		else {
			pval = Alignment.transform10Log10(pval);
			if (Double.isInfinite(pval) || pval > 150) pval = 150;
		}
		String pvalString = Num.formatNumber(pval, 1);

		System.out.println(x+" "+(1-Stat.chiSquareCDF(x, 1))+" "+pval);
	}


		float[] x = { 0,9,99};
		Num.scale1To1000(x);
		Misc.printArray(x);


		try {


			double[] y = {1142,30,20,10,9,8,7,6,5,  30,20,10,9,8,7,6,5};
			double[] x = {968, 0,0,0,0,0,0,0,0,0,   1,1,1,1,1,1,1,1,1,1};
			double n = 10000000;
			double n2 = n * 2;

			System.out.println("Y\tX\tDiff\tAN\tScore\tPValue");

			for (int i=0; i< y.length; i++){
				double sum = x[i]+y[i];
				double an = sum/2.0;

				//calculate pvalue
				double pval = 1;
				//if (y[i] > x[i]){
					PoissonDistributionImpl p = new PoissonDistributionImpl(an);
					pval = 1-p.cumulativeProbability(y[i]-1);
				//}

				//calculate the score
				double score = (y[i]- x[i]) / Math.pow(sum,0.5);

				//print results
				System.out.println(y[i]+"\t"+x[i]+"\t"+(y[i]-x[i])+"\t"+an + "\t"+score+"\t"+pval);
			}
		} catch (Exception e){
			e.printStackTrace();
		}



		//double[] pVals = Num.poissonPValues(50, 1, new File("/usr/bin/R"), new File("/Users/nix/Desktop"));
		try {

			PoissonDistributionImpl p = new PoissonDistributionImpl(1);
			for (int i=0; i< 50; i++){
				System.out.println("**** "+i);
				//System.out.println(1.0- p.cumulativeProbability(i)+"\t"+pVals[i]);

			}

		} catch (Exception e){
			e.printStackTrace();
		}

		double[] pValues = Num.poissonPValues(20000, 1);
		Num.convertToNeg10Log10(pValues);
		for (int i=0; i< pValues.length; i++){
			System.out.println((i)+"\t"+pValues[i]);
		}


		double[] y = {500, 50,6,5,10,10,20};
		double[] x = {495, 45,1,0,0,1,1};
		double n = 10000000;
		double m = 10000000;
		double l = 0.9 * 300000.0;

		System.out.println("Y\tX\tDiff\tScore\tSqr(an)");

		for (int i=0; i< y.length; i++){

			double an = n * (x[i] + 1) / (m + l);

			double score = (y[i]- an)/Math.pow(an, 0.5);

			System.out.println(y[i]+"\t"+x[i]+"\t"+(y[i]-x[i])+"\t"+score+"\t"+Math.pow(an, 0.5));
		}


	 */



	/*
		int[] x = new int[1000000];
		for (int i=0; i< x.length; i++){
			x[i] = i;
		}
		File binary = new File("binary");
		Num.writeBinaryIntArray(x, binary);



		//load
		double[][] matrix = Num.loadDoubleMatrix(new File ("/Users/nix/Desktop/Working/sameOppTotal.txt"));
		//pull first two columns
		double[] values = new double[matrix.length];
		double[] scores = new double[matrix.length];
		double min = matrix[0][0];
		double max = matrix[0][0];
		for (int i=0; i< matrix.length; i++){
			values[i] = matrix[i][0];
			scores[i] = matrix[i][1];
			//System.out.println(values[i]+"\t"+scores[i]);
			if (values[i]< min) min = values[i];
			else if (values[i]> max) max = values[i];
		}

		Histogram h = new Histogram (min, max, 10);
		h.countAll(values, scores);
		h.printBins();
	 */
	/*
		//MultiFastaParser p = new MultiFastaParser (new File ("/Users/nix/HCI/PIs/Olivera/primerTrimName.fasta"), 30, true);
		//p.printMFP();
		File[] files = IO.extractFiles(new File ("/Users/nix/HCI/PIs/Olivera/CPulicariusKnownSeqs/StringentBlastResults/2xHits/"));
		for (int i=0; i< files.length; i++){
		HashSet hash = IO.loadFileIntoHashSet(files[i]);
		Misc.printHashSetToFile(hash, files[i]+".uni");
		System.out.println(files[i].getName()+"\t"+hash.size());
		}



		Pattern pat = Pattern.compile("gb_(.+)_(\\d).*");
		Matcher mat = pat.matcher("gb_sg_1_genome");
		mat.matches();
		System.out.println(mat.group(1)+" "+mat.group(2));

		HashSet names = new HashSet(10000);
		try{
			File file = new File("/users/nix/Desktop/delme.xlt");
			BufferedReader in = new BufferedReader(new FileReader(file));
			String line;
			int counter =0;
			while ((line = in.readLine())!=null){
				line = line.trim();
				if (line.length() ==0) continue;
				if (names.contains(line)) {
					System.out.println("dup "+line);
					counter++;
				}
				names.add(line);
			}
			System.out.println(counter);
		}catch(Exception e){
			e.printStackTrace();
		}


		File f = new File ("/Users/nix/HCI/PIs/Cairns/PombeTranscriptome/StatsOnAnno/IntsForDiffIntergenicClasses100Hits/Ints/Intergenic/240Intergenic.txt");
		double[] values = Num.loadDoubles(f);
		for (int x=0; x< values.length; x++) values[x] = Num.antiLog(values[x], 2);

		double[] thresholds = {-50, 12, 30, 60, 120};
		for (int i=0; i< thresholds.length; i++){
			int counter = 0;
			System.out.print(thresholds[i]+"\t");
			for (int x=0; x< values.length; x++) if (values[x]>= thresholds[i]) counter++;
			System.out.println(counter);
		}

		File[] f = IO.extractFiles(new File ("/Users/nix/HCI/PIs/Cairns/PombeTranscriptome/StatsOnAnno/IntsForDiffClasses100Hits/ParsedOligoDataForDiffExonSubClasses/Ints/"), ".txt");
		for (int i=0; i< f.length; i++){

			double[] values = Num.loadDoubles(f[i]);
			System.out.println(f[i].getName()+"\t"+values.length);
			//delog
			float[] fValues = new float[values.length];
			for (int x=0; x< values.length; x++) fValues[x] = new Double(Num.antiLog(values[x], 2)).floatValue();
			Num.statFloatArray(fValues, false);
		}

		String[] lines = IO.loadFile(new File("/Users/nix/HCI/PIs/Mango/GenLists/joined.txt"));

		Pattern pat = Pattern.compile(".+(WBGene\\d+).*");
		Matcher mat;
		String[] tokens;
		for (int x=0; x< lines.length; x++){
			tokens = lines[x].split("\\s");
			mat = pat.matcher(lines[x]);
			if (mat.matches()){
				System.out.println(tokens[0]+"\t"+mat.group(1));
			}
		}



		//for concatinating float arrays stored as serialized objects

		File[] files = IO.extractFiles(new File(args[0]));
		int numFiles = files.length;
		float[][] fs = new float[numFiles][];
		for (int i=0; i< numFiles; i++){
			fs[i] = (float[])IO.fetchObject(files[i]);
		}
		float[] concat = Num.concatinate(fs);
		IO.saveObject(new File(files[0].getParentFile(),"concat"), concat);
	 */

	/*
		HashSet map = IO.loadFileIntoHashSet(new File(args[0]));
		Iterator it = map.iterator();
		while (it.hasNext()){
			String key = (String)it.next();
			System.out.println(key);
		}
	 */
	/*
		Pattern chromPosPat = Pattern.compile(".*mRNA\\s([SP|chrM].+)\\s;.+");
		Matcher mat = chromPosPat.matcher("mRNA PSAC212.11 ; colour \"2\" ;");
		if (mat.matches()){
			System.out.println(mat.group(1));
		}

		HashSet set = IO.loadFileIntoHashSet(new File("/Users/nix/Desktop/seqs1.txt"));
		Iterator it = set.iterator();
		while (it.hasNext()){
			String x = (String)it.next();
			//System.out.println(Seq.reverseComplementDNA(x));
			//System.out.println(x);
			System.out.println(new StringBuffer(x).reverse());
		}

		LinkedHashMap map = IO.loadKeyValueSpaceTabDelimited(new File("/Users/nix/Desktop/zebCtrls2.tpmap"));
		Iterator it = map.keySet().iterator();
		while (it.hasNext()){
			String key = (String)it.next();
			System.out.println(key+"\t"+map.get(key));
		}

		File cela = new File("/Users/nix/HCI/PIs/ColinDale/ACels/5389E1_251565110005_S01_GE2-v5_95_Feb07_1_1_RawG.cela");
		float[][] x = (float[][])IO.fetchObject(cela);
		for (int i=0; i< x.length; i++){
			for (int j=0; j< x[i].length; j++){
				x[i][j] = 1;
			}
		}
		String name = Misc.removeExtension(cela.getName());
		File blanked = new File (cela.getParentFile(), name+"Blk.cela");
		IO.saveObject(blanked, x);
		System.out.println(x[10][10]);
	 */





